package com.cn.controller;

import com.cn.mapper.OrdersMapper;
import com.cn.pojo.Order;
import com.cn.pojo.OrderItem;
import com.cn.service.GoodsService;
import com.cn.service.OrdersService;

import com.cn.utils.OrderNumberGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import java.util.List;

/**
 * @author:王宇航
 * @create: 2023-05-24 23:12
 * @Description:
 */
@RestController //该Controller全部返回JSON格式
@RequestMapping("/order")
@EnableWebMvc
public class OrderController {
    @Autowired
    private OrdersService ordersService;

    @Autowired
    private GoodsService goodsService;
    @Autowired
    OrdersMapper ordersMapper;

    @RequestMapping(value = "/createOrder", method = RequestMethod.POST)
    public Order login(@RequestBody Order order) {
        order.setOrderNo(OrderNumberGenerator.generate());
        ordersService.createOrder(order);
        Order order1 = ordersService.selectOrderNo(order.getOrderNo());
        return order1;
    }

    @RequestMapping(value = "/createOrderItem", method = RequestMethod.POST)
    public void createOrderItem(@RequestBody OrderItem order) {
       ordersService.createOrderItem(order);
    }
    @RequestMapping(value = "/payStatus/{orderNo}", method = RequestMethod.POST)
    public int payStatus(@PathVariable String orderNo) {
        int i = ordersService.payStatus(orderNo);
        return i;
    }

    @RequestMapping(value = "/deleOrder/{orderNo}/{userId}", method = RequestMethod.POST)
    public List<OrderItem> deleOrder(@PathVariable String orderNo, @PathVariable int userId) {
         ordersService.deleOrder(orderNo);
        List<OrderItem> orderItems = goodsService.searchOrderItem(userId);
        return orderItems;
    }
    @RequestMapping(value = "/findOrderMoney/{orderNo}", method = RequestMethod.POST)
    public int findOrderMoney(@PathVariable String orderNo) {
        int orderMoney = ordersService.findOrderMoney(orderNo);
        return orderMoney;

    }
    @RequestMapping(value = "/selectAll", method = RequestMethod.POST)
    public List<Order> selectAll(@RequestBody Order order) {

       return  ordersService.selectAll(order);
    }

    @RequestMapping(value = "/searchOrderDetail/{id}", method = RequestMethod.POST)
    public  List<Order> searchOrderDetail(@PathVariable int id) {
        List<Order> orders = ordersService.searchOrderDetail(id);
        return orders;
    }

    @PostMapping("/close/{orderId}")
    public  void close(@PathVariable int orderId) {
         ordersMapper.close(orderId);
        }
    @PostMapping("/checkOut/{orderId}")
    public  boolean checkOut(@PathVariable int orderId) {
        int i = ordersMapper.orderStatus(orderId);
        if (i==2||i==3){
             ordersMapper.checkOut(orderId);
            return true;
        }else {
            return false;
        }

    }
    @PostMapping("/checkDone/{orderId}")
    public  boolean checkDone(@PathVariable int orderId) {
        int i = ordersMapper.orderStatus(orderId);
        if (i==2){
            ordersMapper.checkDone(orderId);
            return true;
        }else {
            return false;
        }
    }
}